/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.pps.services;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import vn.com.pps.dao.ICourseCreditMemoDAO;
import vn.com.pps.dao.ICourseDAO;
import vn.com.pps.dao.ICourseFeeDAO;
import vn.com.pps.dao.ICourseLevel;
import vn.com.pps.dao.ICoursePupilDAO;
import vn.com.pps.dao.ICourseTeacherAttendenceDAO;
import vn.com.pps.dao.ICourseTeacherDAO;
import vn.com.pps.dao.IPupilEventDAO;
import vn.com.pps.entities.Course;
import vn.com.pps.entities.CoursePupil;
import vn.com.pps.entities.CoursePupilDetail;
import vn.com.pps.entities.CoursePupilFee;
import vn.com.pps.entities.CourseTeacherAttendance;
import vn.com.pps.entities.CourseTeacherDetail;
import vn.com.pps.entities.CreditMemo;
import vn.com.pps.entities.PupilEvent;
import vn.com.pps.web.bean.course.CoursePupilBean;
import vn.com.pps.web.ui.CoursePupilFeeTableItem;
import vn.com.pps.web.ui.CoursePupilTableItem;
import vn.com.pps.web.ui.CourseTableItem;
import vn.com.pps.web.ui.CourseTeacherTableItem;
import vn.com.pps.web.ui.CreditMemoTableItem;
import vn.com.pps.web.ui.TeacherAttendanceTableItem;

/**
 *
 * @author Dai
 */
public class CourseService implements ICourseService {

    private ICourseCreditMemoDAO courseCreditMemoDAO;
    private ICourseFeeDAO courseFeeDAO;
    private ICourseLevel courseLevel;
    private ICoursePupilDAO coursePupilDAO;
    private ICourseTeacherAttendenceDAO courseTeacherAttendenceDAO;
    private ICourseTeacherDAO courseTeacherDAO;
    private ICourseDAO courseDAO;
    private IPupilEventDAO pupilEventDAO;

    public void setCourseCreditMemoDAO(ICourseCreditMemoDAO courseCreditMemoDAO) {
        this.courseCreditMemoDAO = courseCreditMemoDAO;
    }

    public void setCourseFeeDAO(ICourseFeeDAO courseFeeDAO) {
        this.courseFeeDAO = courseFeeDAO;
    }

    public void setCourseLevel(ICourseLevel courseLevel) {
        this.courseLevel = courseLevel;
    }

    public void setCoursePupilDAO(ICoursePupilDAO coursePupilDAO) {
        this.coursePupilDAO = coursePupilDAO;
    }

    public void setCourseTeacherAttendenceDAO(ICourseTeacherAttendenceDAO courseTeacherAttendenceDAO) {
        this.courseTeacherAttendenceDAO = courseTeacherAttendenceDAO;
    }

    public void setCourseTeacherDAO(ICourseTeacherDAO courseTeacherDAO) {
        this.courseTeacherDAO = courseTeacherDAO;
    }

    public void setCourseDAO(ICourseDAO courseDAO) {
        this.courseDAO = courseDAO;
    }

    public void setPupilEventDAO(IPupilEventDAO pupilEventDAO) {
        this.pupilEventDAO = pupilEventDAO;
    }
    
    

    @Override
    public int countCourse(String whereClause) {
        return courseDAO.getCount(whereClause);
    }

    @Override
    public ArrayList<CourseTableItem> getCourses(int pageNo, int pageSize, String whereClause) {
        ArrayList<CourseTableItem> courseTableItems = new ArrayList<CourseTableItem>();
        for (Course course : courseDAO.getCourses(pageNo, pageSize, whereClause).getPageItems()) {
            courseTableItems.add(new CourseTableItem(course));
        }
        return courseTableItems;
    }

    @Override
    public Course getCourse(Integer id) {
        return courseDAO.getCourse(id);
    }

    @Override
    public ArrayList<CoursePupilTableItem> getPupilByCourse(int pageNo, int pageSize, Integer courseID) {
        System.out.println("@getPupilByCourse:");
        ArrayList<CoursePupilTableItem> coursePupilTableItems = new ArrayList<CoursePupilTableItem>();
        for (CoursePupilDetail coursePupilDetail : coursePupilDAO.getCoursePupils(pageNo, pageSize, courseID).getPageItems()) {
            coursePupilTableItems.add(new CoursePupilTableItem(coursePupilDetail));
        }
        return coursePupilTableItems;
    }

    @Override
    public int countPupilByCourse(Integer courseID) {
        return coursePupilDAO.getCount(courseID);
    }

    @Override
    public CourseTeacherDetail getTeacherByCourse(Integer courseID) {
        return courseTeacherDAO.getTeacherByCourseID(courseID);
    }

    @Override
    public ArrayList<CreditMemoTableItem> getCreditMemosByCourse(int pageNo, int pageSize, Integer courseID) {
        ArrayList<CreditMemoTableItem> creditMemoTableItems = new ArrayList<CreditMemoTableItem>();
        for (CreditMemo creditMemo : courseCreditMemoDAO.getCreditMemos(pageNo, pageSize, courseID).getPageItems()) {
            creditMemoTableItems.add(new CreditMemoTableItem(creditMemo));
        }
        return creditMemoTableItems;
    }

    @Override
    public ArrayList<TeacherAttendanceTableItem> getTakenRecordsByCourse(int pageNo, int pageSize, Integer courseID) {
        ArrayList<TeacherAttendanceTableItem> teacherAttendanceTableItem = new ArrayList<TeacherAttendanceTableItem>();
        for (CourseTeacherAttendance courseTeacherAttendance : courseTeacherAttendenceDAO.getTakenRecordsByCourse(pageNo, pageSize, courseID).getPageItems()) {
            teacherAttendanceTableItem.add(new TeacherAttendanceTableItem(courseTeacherAttendance));
        }
        return teacherAttendanceTableItem;

    }

    @Override
    public int getTakenRecordCount(int courseID) {
        return courseTeacherAttendenceDAO.getTakenRecordCount(courseID);
    }

    @Override
    public int getCreditMemoCountByCourseID(int courseID) {
        return courseCreditMemoDAO.getCreditMemoCountByCourseID(courseID);
    }

    @Override
    public int saveCreditMemo(CreditMemo creditMemo) {
        return courseCreditMemoDAO.insert(creditMemo);
    }

    @Override
    public CoursePupilDetail getCoursePupilDetail(Integer id) {
        return coursePupilDAO.getCoursePupilDetail(id);
    }

    @Override
    public ArrayList<CoursePupilFeeTableItem> getCourseFees(int courseID, int pupilID) {
        ArrayList<CoursePupilFeeTableItem> pupilTableItems = new ArrayList<CoursePupilFeeTableItem>();
        for (CoursePupilFee pupil : courseFeeDAO.getCourseFees(courseID, pupilID)) {
            pupilTableItems.add(new CoursePupilFeeTableItem(pupil));
        }
        return pupilTableItems;
    }

    @Override
    public int update(CoursePupilFee courseFee) {
        return courseFeeDAO.update(courseFee);
    }

    @Override
    public ArrayList<CourseTeacherTableItem> getTeacherByCourse(int pageNo, int pageSize, Integer TeacherID) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ArrayList<CourseTeacherTableItem> getCoursesByTeacher(int pageNo, int pageSize, Integer TeacherID) {
        ArrayList<CourseTeacherTableItem> courseTeacherTableItems = new ArrayList<CourseTeacherTableItem>();
        for (CourseTeacherDetail courseTeacherDetail : courseTeacherDAO.getCoursesByTeacher(pageNo, pageSize, TeacherID).getPageItems()) {
            courseTeacherTableItems.add(new CourseTeacherTableItem(courseTeacherDetail));
        }
        return courseTeacherTableItems;
    }

    @Override
    public int getCountCoursesByTeacher(Integer teacherID) {
        return courseTeacherDAO.getCountCourseByTeacher(teacherID);
    }

    @Override
    public int getCountCoursesByPupil(Integer PupilID) {
        return coursePupilDAO.getCountCoursesByPupil(PupilID);
    }

    @Override
    public ArrayList<CoursePupilTableItem> getCoursesByPupil(int pageNo, int pageSize, Integer PupilID) {
        ArrayList<CoursePupilTableItem> coursePupilTableItems = new ArrayList<CoursePupilTableItem>();
        for (CoursePupilDetail coursePupilDetail : coursePupilDAO.getCourseByPupils(pageNo, pageSize, PupilID).getPageItems()) {
            coursePupilTableItems.add(new CoursePupilTableItem(coursePupilDetail));
        }
        return coursePupilTableItems;
    }

    @Override
    public int saveTeacherAttendance(CourseTeacherAttendance courseTeacherAttendance) {
        return courseTeacherAttendenceDAO.insert(courseTeacherAttendance);
    }

    @Override
    public int editTeacherAttendance(CourseTeacherAttendance courseTeacherAttendance) {
        return courseTeacherAttendenceDAO.update(courseTeacherAttendance);
    }

    @Override
    public CourseTeacherAttendance getTeacherAttendanceByID(int id) {
        return courseTeacherAttendenceDAO.getCourseTeacherAttendance(id);
    }

    @Override
    public int deleteteacherAttendance(int id) {
        return courseTeacherAttendenceDAO.delete(id);
    }

    @Override
    public int editCourseCreditMemo(CreditMemo CreditMemo) {
        return courseCreditMemoDAO.update(CreditMemo);
    }

    @Override
    public int deleteCourseCreditMemo(int id) {
        return courseCreditMemoDAO.delete(id);
    }

    @Override
    public CreditMemo getCreditMemo(int id) {
        return courseCreditMemoDAO.getCreditMemo(id);
    }

    @Override
    public int editChangeCourse(CoursePupil coursePupil, int toCourseID) {
        System.out.println("@editChangeCourse");
        String fromCourseName=courseDAO.getCourse(coursePupil.getCourseID()).getCourseName();
        String toCourseName=courseDAO.getCourse(toCourseID).getCourseName();
        Calendar calendar = Calendar.getInstance();
        java.sql.Timestamp currentTimeStamp = new java.sql.Timestamp(calendar.getTime().getTime());
        coursePupil.setStatus("C");
        coursePupil.setNote("Chuyển sang lớp "+toCourseName);
        coursePupil.setLastModified(currentTimeStamp);
        coursePupilDAO.update(coursePupil);
        
        //Insert new coursepupilID
        coursePupil.setStatus("A");
        coursePupil.setCourseID(toCourseID);
        coursePupil.setRegistedOn(currentTimeStamp);
        coursePupil.setLastModified(currentTimeStamp);
        int coursePupilID = coursePupilDAO.insert(coursePupil);
        //Add event for changing course
        PupilEvent event = new PupilEvent();
        event.setPupilID(coursePupil.getPupilID());
        
        
        event.setEvent("Chuyển từ lớp " +fromCourseName  + " sang lớp " +toCourseName );
        event.setCreatedDate(currentTimeStamp);
        pupilEventDAO.insert(event);
        return coursePupilID;
    }

    @Override
    public int saveCourse(Course course) {
        return courseDAO.insert(course);
    }

    @Override
    public int editCourse(Course course) {
        return courseDAO.update(course);
    }

}
